package com.amqr.mediaplayer.activity;

import android.Manifest;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;

import com.amqr.mediaplayer.R;

import permissions.dispatcher.NeedsPermission;
import permissions.dispatcher.OnNeverAskAgain;
import permissions.dispatcher.OnPermissionDenied;
import permissions.dispatcher.OnShowRationale;
import permissions.dispatcher.PermissionRequest;
import permissions.dispatcher.RuntimePermissions;

/**
 * User: LJM
 * Date&Time: 2017-06-26 & 20:01
 * Describe: Describe Text
 */
@RuntimePermissions
public class IndexActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_index);

        findViewById(R.id.mTv1).setOnClickListener(this);
        findViewById(R.id.mTv2).setOnClickListener(this);
        findViewById(R.id.mTv3).setOnClickListener(this);
        findViewById(R.id.mTv4).setOnClickListener(this);


    }

    @Override
    public void onClick(View view) {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            IndexActivityPermissionsDispatcher.needPermWithCheck(IndexActivity.this);
        }else{
            switch (view.getId()){
                case R.id.mTv1:
                    startActivity(new Intent(IndexActivity.this,SimpleVideoActivity.class));
                    break;
                case R.id.mTv2:
                    startActivity(new Intent(IndexActivity.this,ScaleAutoPlayVideoActivity.class));
                    break;
                case R.id.mTv3:
                    startActivity(new Intent(IndexActivity.this,TimePlayVideoActivity.class));
                    break;
                case R.id.mTv4:
                    startActivity(new Intent(IndexActivity.this,MainActivity.class));
                    break;
            }
        }
    }


    @NeedsPermission({Manifest.permission.READ_EXTERNAL_STORAGE})
    void needPerm() {

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        IndexActivityPermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults);
    }

    @OnShowRationale({Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE})
    void showRationalePerm(final PermissionRequest request) {
        Toast.makeText(IndexActivity.this,"少年需要存储权限呀，待会要读本地内容啊",Toast.LENGTH_SHORT).show();
    }

    @OnPermissionDenied({Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE})
    void deniedPerm() {
        Toast.makeText(IndexActivity.this,"你拒绝了权限，自己去设置开，我就不给你跳了",Toast.LENGTH_SHORT).show();
    }

    @OnNeverAskAgain({Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE})
    void askAgainPerm() {
        openAppSetting("要授权啦");
    }


    protected void openAppSetting(String content) {
        new AlertDialog.Builder(this)
                .setMessage(content)
                .setPositiveButton("设置", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        // 跳转到app设置
                        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                        intent.setData(Uri.parse("package:" + getPackageName()));
                        startActivity(intent);
                    }
                })
                .create()
                .show();
    }


}
